import {
    apiGetSmDictionaryController,
    apiPostSmDictionaryController,
} from "./apiSmDictionaryController"

//获取服务管理平台字典树
export const computedPostSmDictionaryTreeSearch = (ev) => {
    apiPostSmDictionaryController('dictionary/tree/search',{})
    .then(
        (res) =>{
            if(res.data.code==="S100101"){
                // console.log(res.data.data.list)
                ev.parentOptions = res.data.data.list;
            }else{
                console.log('api-dictionary/tree/search连接不成功');
            }
        }
    )
}

//类型查询服务管理平台字典
export const computedPostSmDictionaryTypeSearch = (ev,type,pageNum) => {
    apiPostSmDictionaryController('dictionary/search',{
        "type": type,
        "pageSize": 10,
        "pageNum": pageNum
    })
    .then(
        (res) =>{
            if(res.data.code==="S100101"){
                ev.tableData = res.data.data.list;
                ev.pageTotal = res.data.data.total;
            }else{
                console.log('api-dictionary/search连接不成功');
            }
        }
    )
}

//单个字典查询
export const computedDictionarySearch = (ev,id,type) => {
    apiPostSmDictionaryController('dictionary/search',{
        "type": type,
        "id": id,
    })
    .then(
        (res) =>{
            if(res.data.code==="S100101"){
                // console.log(res.data.data.list[0]);
                ev.singeOption = res.data.data.list[0];
            }else{
                console.log('连接不成功');
            }
        }
    )
}

//添加服务管理平台字典
export const computedPostSmDictionaryAdd = (ev,obj) =>{
    apiPostSmDictionaryController('dictionary/save',{
        "parentId": obj.id,
        "order": obj.order,
        "name": obj.name,
        "key": obj.key
    })
    .then(
        (res) =>{
            if(res.data.code==="S100101"){
                ev.$message({
                    message: "添加成功",
                    type: "success"
                });
                ev.refresh();
            }else{
                if(res.data.code==="SM200204"){
                    ev.$message({
                        message:"此名字已存在,请修改",
                        type:"error"
                    })
                }else if(res.data.code==="SM200210"){
                    ev.$message({
                        message:"此英文值已存在,请修改",
                        type:"error"
                    })
                }else{
                    ev.$message({
                        message:"添加失败",
                        type:"error"
                    })
                }   
            }
        }
    )
}

//更新服务管理平台字典
export const computedPostSmDictionaryEidt = (ev,obj) =>{
    apiPostSmDictionaryController('dictionary/update',{
        "id": obj.id,
        "order": obj.order,
        "name": obj.name,
        "key": obj.key
    })
    .then(
        (res) =>{
            if(res.data.code==="S100101"){
                ev.$message({
                    message: "修改成功",
                    type: "success"
                });
                ev.refresh();
            }else{
                if(res.data.code==="SM200204"){
                    ev.$message({
                        message:"此名字已存在,请修改",
                        type:"error"
                    })
                }else if(res.data.code==="SM200210"){
                    ev.$message({
                        message:"此英文值已存在,请修改",
                        type:"error"
                    })
                }else{
                    ev.$message({
                        message:"添加失败",
                        type:"error"
                    })
                }
            }
        }
    )
}

//删除服务管理平台字典
export const computedGetSmDictionaryDel = (ev,id) =>{
    apiGetSmDictionaryController('dictionary/'+id+'/delete'
    ).then(
        (res) =>{
            if(res.data.code==="S100101"){
                // console.log(res.data)
                ev.$message({
                    message: "成功删除",
                    type: "success"
                });
                ev.refresh();
            }else{
                console.log('删除不成功');
            }
        }
    )
}

